#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
Simplified BSD License (BSD)
Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this 
      list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice, 
      this list of conditions and the following disclaimer in the documentation 
      and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

#region Record of Change
//             							CHANGE HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | DEVELOPER  | DESCRIPTION                                              |
// -------------------------------------------------------------------------------------
// | 14-Nov-09 | HaiNM      | First creation.                                          |
// -------------------------------------------------------------------------------------
//
#endregion

#region Record of Audit
//             							REVIEW HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | REVIEWER   | NOTES                                                    |
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
//
#endregion

#region Using directives
using System;

using Thn;
#endregion

namespace Thn.Interface.Vcl
{
    /// <summary>
    /// Default implementation of <see cref="IPageHistory"/>.
    /// </summary>
#if DEBUG
    public
#else
    internal
#endif
 class PageHistory : IPageHistory
    {
        #region Fields
        IPageDescriptor[] mPages;
        #endregion

        #region Properties
        #endregion

        #region Ensure Capacity
        /// <summary>
        /// Ensure that internal buffer have enough space.
        /// </summary>
        void EnsureCapacity(int capacity)
        {
            if ((mPages == null) || (mPages.Length < capacity))
            {
                mPages = new IPageDescriptor[capacity];
            }
            #region Defensive tracing
            else
            {
                //already have enough space.
            }
            #endregion
        }
        #endregion

        #region Press
        /// <summary>
        /// Press the stack down by removing items at the bottom.
        /// </summary>
        /// <param name="removeCount">Number of items to be removed.</param>
        void Press(int removeCount)
        {
            if (mCount >= removeCount)
            {
                #region Have enough space

                //copy items and adjust count
                Array.Copy(mPages, removeCount, mPages, 0, mCount - removeCount);
                mCount--;
                #endregion
            }
            else
            {
                #region Not enough space
                throw new Exception("No room left for pressing");
                #endregion
            }
        }
        #endregion

        #region IPageHistory Members

        #region Count
        private int mCount;
        /// <summary>
        /// Gets the number of items contained.
        /// </summary>
        public int Count
        {
            get { return mCount; }
        }
        #endregion

        #region Indexer
        /// <summary>
        /// Gets an item at the specified location index.
        /// </summary>
        /// <param name="index">Zero-based location index.</param>
        public IPageDescriptor this[int index]
        {
            get { return mPages[index]; }
        }
        #endregion

        #region Current
        private IPageDescriptor mCurrent;
        /// <summary>
        /// Gets the item on top of the stack.
        /// </summary>
        public IPageDescriptor Current
        {
            get { return mCurrent; }
        }
        #endregion

        #region Push
        /// <summary>
        /// Push an item on the history stack.
        /// </summary>
        public void Push(IPageDescriptor item)
        {
            //if not enough space then press for more space
            if (mCount >= mCapacity) Press(1);
            #region Defensive tracing
            else
            {
                //already have enough space
            }
            #endregion

            //put the new item on top and make it current
            mPages[mCount] = item;
            mCurrent = item;
            mCount++;
        }
        #endregion

        #region Pop
        /// <summary>
        /// Remove the top item off the stack.
        /// </summary>
        public IPageDescriptor Pop()
        {
            IPageDescriptor result = null;
            if (mCount > 0)
            {
                #region Has at least one item
                
                result = mCurrent;
                mCount--;
                if (mCount > 0) mCurrent = mPages[mCount - 1];
                else mCurrent = null;
                
                #endregion
            }
            else
            {
                #region No more item to pop
                //no items left for popping, return null
                #endregion
            }

            return result;
        }
        #endregion

        #region Capacity
        private int mCapacity = 10;
        /// <summary>
        /// Gets/Sets the maximum number of items to keep track of.
        /// <para>When pushing more item over this limit, the ones under the bottom will be removed.</para>
        /// </summary>
        public int Capacity
        {
            get { return mCapacity; }
            set
            {
                if (mCapacity != value)
                {
                    mCapacity = value;
                    EnsureCapacity(mCapacity);
                }
                #region Defensive tracing
                else
                {
                    //value is not different
                }
                #endregion
            }
        }
        #endregion

        #region Clear
        /// <summary>
        /// Empty this stack.
        /// </summary>
        public void Clear()
        {
            Array.Clear(mPages, 0, mPages.Length);
            mCount = 0;
            mCurrent = null;
        }
        #endregion

        #endregion

        #region Constructors
        /// <summary>
        /// Default constructor.
        /// </summary>
        public PageHistory()
        {
            EnsureCapacity(mCapacity);
        }
        #endregion
    }
}
